package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.IPolicyDao;
import org.gxuwz.core.entity.TPolicy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("PolicyDao")
public class PolicyDao extends BaseDao implements IPolicyDao{
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}
	@Override
	public void addPolicy(TPolicy poli) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(poli);
		tx.commit();
		session.close();
	}
	@Override
	public List<TPolicy> getAll(String runyear, String maxline) {
		List<TPolicy> list = null;
		Session session = getSession();
		String hql = "select * from t_area where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(runyear!=null&&maxline!=""){
			hql += " and runyear like '%"+runyear+"%'";
		}
		if(maxline!=null&&maxline!=""){
			hql += " and areaname like '%"+maxline+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TPolicy.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}
	@Override
	public TPolicy getByRunYear(String runyear) {
		TPolicy poli = null;
		List<TPolicy> list = null;
		Session session = getSession();
		String hql = "select * from t_policy where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(runyear!=null&&runyear!=""){
			hql += " and runyear like '%"+runyear+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TPolicy.class);
		list = query.list();
		session.close();
		if(list.size()>=0){
			poli =list.get(0);
		}
	    return poli;
	}
	
	@Override
	public void update(TPolicy poli) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(poli);
		tx.commit();
		session.close();
	}
	@Override
	public void delete(TPolicy poli) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(poli);
		tx.commit();
		session.close();
		
	}
}
